//
//  HistoryTableViewController.swift
//  BookStore
//
//  Created by JinYuXin on 2022/5/11.
//

import UIKit

class HistoryTableViewController: UITableViewController {
    
    let defaults = UserDefaults.standard
    var historylistid:String=""
    var listinfo=[[String:String]]()

    override func viewDidLoad() {
        super.viewDidLoad()
        getListInfo()
        let xib = UINib(nibName: "HistoryTableViewCell", bundle: nil)
        tableView.register(xib, forCellReuseIdentifier: "historycell")
        tableView.rowHeight = 130
    }

    override func viewWillAppear(_ animated: Bool) {
        getListInfo()
        tableView.reloadData()
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listinfo.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "historycell", for: indexPath) as! HistoryTableViewCell
        
        cell.booknum.text = listinfo[indexPath.row]["num"]
        cell.time.text = listinfo[indexPath.row]["time"]
        cell.listid.text = listinfo[indexPath.row]["listid"]
        cell.bookprice.text = listinfo[indexPath.row]["price"]

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let controller:HistoryDetailViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "historydetail") as! HistoryDetailViewController
        controller.listtime=listinfo[indexPath.row]["time"]!
        controller.bookprice=listinfo[indexPath.row]["price"]!
        controller.booknum=listinfo[indexPath.row]["num"]!
        controller.listid0=listinfo[indexPath.row]["listid"]!
        controller.booksid=listinfo[indexPath.row]["bookid"]!
        self.present(controller, animated: true, completion: nil)
    }
    
    func getListInfo()
    {
        let user = defaults.string(forKey: "userName")!
        let queryuser = UserTable.getUserId(user)
        let userid = String(queryuser![0]["id"]! as! Int)
        let queryResult = HistoryTable.getListInfo(userid)
        let a = queryResult as! [[String:String]]
        listinfo = a

    }
    
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
